#include "Value.h"
#include "VarTable.h"
#include <iostream>
#include <string>
#include <cassert>
using namespace std;
int main()
{
	VarTable::init();
	VarTable* t = VarTable::getInstance();
	ValueInt* exp1 = new ValueInt(23);
	ValueInt* exp2 = new ValueInt(1481);//which is better?
	string sa("a"), shaha("haha");
	// a = 23
	ValueInt* pv = t -> get(sa);
	if (pv == NULL)
	{
		t -> def(sa, *exp1);//delete exp1 will raise an error!!!
	}
	else
	{
		assert(pv -> getType() == exp1 -> getType());
		pv -> assign(*exp1);
	}
	//delete pv may raise an error!!!
	// a
	pv = t -> get(sa);
	if (pv == NULL)
	{
		cout << "Error: " << sa << " not defined!" << endl;
	}
	else
	{
		cout << pv -> str() << endl;
	}
	//don't delete pv!!!
	//haha = a * 1481
	pv = t -> get(sa);
	if (pv == NULL)
	{
		cout << "Error: " << sa << " not defined!" << endl;
	}
	else
	{
		ValueInt* tmp = new ValueInt();
		tmp -> assign(pv -> mul(*exp2));
		Value* pv2 = t -> get(shaha);
		if (pv2 == NULL)
		{
			t -> def(shaha, *tmp);
		}
		else
		{
			pv2 -> assign(*tmp);
		}
	}
	//haha
	pv = t -> get(shaha);
	if (pv == NULL)
	{
		cout << "Error: " << shaha << " not defined!" << endl;
	}
	else
	{
		cout << pv -> str() << endl;
	}
	cout << "TEST END." << endl;
	return 0;
}

